﻿// <copyright file="TimesPeopleNewsfeed.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-05-07</date>
// <summary>TimesPeopleNewsfeed class definition</summary>

namespace NYTimes
{
    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Xml.Linq;
    using NYTimes.Core;
    using NYTimes.Data;

    /// <summary>
    /// New York Times TimesPeople Newsfeed API
    /// </summary>
    public class TimesPeopleNewsfeed : TimesPeople
    {
        /// <summary>
        /// Initializes a new instance of the TimesPeopleNewsfeed class.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors", Justification = "This is calling the ParseResultSet() function in this class.")]
        public TimesPeopleNewsfeed()
        {
            this.Activities = new ObservableCollection<Activity>();

            if (SampleData.IsInDesignTool)
            {
                var xml = Core.SampleData.GetSampleData("TimesPeopleNewsfeed.xml");

                if (!string.IsNullOrEmpty(xml))
                {
                    var doc = XDocument.Parse(xml);

                    var resultSet = doc.Element("result_set");

                    this.ParseResultSet(resultSet);
                }
            }
        }

        /// <summary>
        /// Gets the "newsfeed" data type
        /// </summary>
        public override string DataType
        {
            get { return "newsfeed"; }
        }

        /// <summary>
        /// Gets the activities as a collection of <see cref="NYTimes.Data.Activity"/> objects.
        /// </summary>
        public ObservableCollection<Activity> Activities { get; private set; }

        /// <summary>
        /// Parse the activities
        /// </summary>
        /// <param name="resultSet">the result set</param>
        public override void ParseResultSet(System.Xml.Linq.XElement resultSet)
        {
            ParseActivities(resultSet, this.Activities);
        }

        /// <summary>
        /// Clear the activity list
        /// </summary>
        /// <param name="failure">the web client error</param>
        public override void OnException(Exception failure)
        {
            this.Activities.Clear();
        }
    }
}
